package com.example.hellocharts;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;

import java.util.ArrayList;
import java.util.List;

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        BarChart barChart = findViewById(R.id.barchart);
        List<IBarDataSet> s = new ArrayList<>();

        List<BarEntry> bar1 = new ArrayList<>();
        bar1.add(new BarEntry(0, 39f));
        bar1.add(new BarEntry(1, 87f));
        bar1.add(new BarEntry(2, 60f));
        bar1.add(new BarEntry(3, 70f));
        bar1.add(new BarEntry(4, 37f));
        BarDataSet barDataSet1 = new BarDataSet(bar1, "");
        barDataSet1.setValueTextColor(Color.BLUE); // 值的颜色
        barDataSet1.setValueTextSize(15f); // 值的大小
        barDataSet1.setColor(Color.parseColor("#FF0000")); // 柱子的颜色
        barDataSet1.setLabel("水果"); // 设置标签之后，图例的内容默认会以设置的标签显示
        // 设置柱子上数据显示的格式
        s.add(barDataSet1);

        List<BarEntry> bar2 = new ArrayList<>();
        bar2.add(new BarEntry(0.3f, 21f));
        bar2.add(new BarEntry(1.3f, 45f));
        bar2.add(new BarEntry(2.3f, 43f));
        bar2.add(new BarEntry(3.3f, 44f));
        bar2.add(new BarEntry(4.3f, 18f));
        BarDataSet barDataSet2 = new BarDataSet(bar2, "");
        barDataSet2.setValueTextColor(Color.BLUE); // 值的颜色
        barDataSet2.setColor(Color.parseColor("#D1B1F1"));
        barDataSet2.setLabel("蔬菜");
        s.add(barDataSet2);

        BarData barData = new BarData(s);
        barData.setBarWidth(0.3f); // 设置柱子的宽度
        barChart.setData(barData);
    }
}